package com.fmboy.MXH.nav.domain.Result;

import com.fmboy.MXH.nav.domain.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 *
 * @param <T>
 */

@Data
public class Result<T> implements Serializable {
    private Integer status; // 编码：参见 ErrorCode 枚举
    private String message; // 错误信息
    private T data; // 数据

    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setStatus(ErrorCode.SUCCESS.getCode());
        result.setMessage(ErrorCode.SUCCESS.getMessage());
        return result;
    }

    public static <T> Result<T> success(String message) {
        Result<T> result = new Result<>();
        result.setStatus(ErrorCode.SUCCESS.getCode());
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> success(String message, T object) {
        Result<T> result = new Result<>();
        result.setData(object);
        result.setStatus(ErrorCode.SUCCESS.getCode());
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> error(ErrorCode errorCode) {
        Result<T> result = new Result<>();
        result.setStatus(errorCode.getCode());
        result.setMessage(errorCode.getMessage());
        return result;
    }

    public static <T> Result<T> error(ErrorCode errorCode, String message) {
        Result<T> result = new Result<>();
        result.setStatus(errorCode.getCode());
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setStatus(ErrorCode.INTERNAL_ERROR.getCode()); // 默认设置为内部错误
        result.setMessage(message);
        return result;
    }

}